package OT.shoufouni.mainGUI;


import OT.shoufouni.R;
import OT.shoufouni.serverThreads.serverAddFriendQuaryThread;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EntryAddActivity extends Activity {
		EditText editTextName;
		EditText editTextaddress;
		EditText editTexttel;
		
		String myName;
    	String myAddress;
    	String myTel;
		
		private Handler mHandler = new Handler();
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
        editTextName = (EditText) findViewById(R.id.edit_text_name);
        editTextaddress = (EditText)findViewById(R.id.edit_text_address);
        editTexttel = (EditText)findViewById(R.id.edit_text_tel_number);
        Button buttonCommit = (Button)findViewById(R.id.button_commit);
        Button buttonCancel = (Button)findViewById(R.id.button_cancel);
        MyClickInterface buttonListener = new MyClickInterface(); 
        CancelClickInterface cancelButtonListner = new CancelClickInterface();
        buttonCommit.setOnClickListener(buttonListener);
        buttonCancel.setOnClickListener(cancelButtonListner);
    }
    public class MyClickInterface implements View.OnClickListener{
    	@Override
    	public void onClick(View v){
	    	myName = editTextName.getText().toString();
	    	myAddress =  editTextaddress.getText().toString();
	    	myTel =  editTexttel.getText().toString();
	    	if ( (myName.equals(""))||(myAddress.equals(""))||(myTel.equals("")) ){
	    		Toast.makeText(v.getContext(),"Arguments are missing \nTry again",Toast.LENGTH_LONG).show();
	    		return;
	    	}
	    	
	    	Context context = getApplicationContext();
     	   	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            String IdMail = prefs.getString("ID_MAIL", "");
            
            Toast.makeText(getBaseContext(),"Connecting to the server"+"\nPlease wait",Toast.LENGTH_LONG).show();
	    	// connect to server in order to add the new friend
	    	serverAddFriendQuaryThread quaryThread = 
	        	  new serverAddFriendQuaryThread(EntryAddActivity.this, IdMail,myAddress);
		    quaryThread.start(); 
     	}
    }
    public class CancelClickInterface implements View.OnClickListener{
    	@Override
    	public void onClick(View v){
			Intent intent = new Intent();
			setResult(RESULT_CANCELED,intent);
			finish();
     	}
    }
    
    public void responseDisplay(final String ans){
	   	mHandler.post(new Runnable() {
	           public void run() {
	        	   // the new user was created
	        	   if ("GOOD".equals(ans)){
	        		    Intent intent = new Intent();
		       			intent.putExtra("name", myName );
		       			intent.putExtra("address", myAddress);
		       			intent.putExtra("tel", myTel);
		       			setResult(RESULT_OK,intent);
		       			finish();   
	        	   }
	        	   else if("BAD".equals(ans)){
	        		   Toast.makeText(getBaseContext(),"Email does not exists\n try again",Toast.LENGTH_LONG).show();
	        	   }
	        	   else{
	        		   Toast.makeText(getBaseContext(),"Server connection problem\n try again",Toast.LENGTH_LONG).show();
	        	   }
	           }
	   });
	}
}
